{
  "cells": [
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "%matplotlib inline"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\n# Contour fit\n\nTwo type of fit :\n - Ellipse\n - Circle\n\nIn the two case we use a least square algorithm\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "from matplotlib import pyplot as plt\nfrom numpy import cos, linspace, radians, sin\n\nfrom py_eddy_tracker import data\nfrom py_eddy_tracker.generic import coordinates_to_local, local_to_coordinates\nfrom py_eddy_tracker.observations.observation import EddiesObservations\nfrom py_eddy_tracker.poly import fit_circle_, fit_ellipse"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "Load example identification file\n\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "a = EddiesObservations.load_file(data.get_demo_path(\"Anticyclonic_20190223.nc\"))"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "Function to draw circle or ellipse from parameter\n\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "def build_circle(x0, y0, r):\n    angle = radians(linspace(0, 360, 50))\n    x_norm, y_norm = cos(angle), sin(angle)\n    return local_to_coordinates(x_norm * r, y_norm * r, x0, y0)\n\n\ndef build_ellipse(x0, y0, a, b, theta):\n    angle = radians(linspace(0, 360, 50))\n    x = a * cos(theta) * cos(angle) - b * sin(theta) * sin(angle)\n    y = a * sin(theta) * cos(angle) + b * cos(theta) * sin(angle)\n    return local_to_coordinates(x, y, x0, y0)"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "Plot fitted circle or ellipse on stored contour\n\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "xs, ys = a.contour_lon_s, a.contour_lat_s\n\nfig = plt.figure(figsize=(15, 15))\n\nj = 1\nfor i in range(0, 800, 30):\n    x, y = xs[i], ys[i]\n    x0_, y0_ = x.mean(), y.mean()\n    x_, y_ = coordinates_to_local(x, y, x0_, y0_)\n    ax = fig.add_subplot(4, 4, j)\n    ax.grid(), ax.set_aspect(\"equal\")\n    ax.plot(x, y, label=\"store\", color=\"black\")\n    x0, y0, a, b, theta = fit_ellipse(x_, y_)\n    x0, y0 = local_to_coordinates(x0, y0, x0_, y0_)\n    ax.plot(*build_ellipse(x0, y0, a, b, theta), label=\"ellipse\", color=\"green\")\n    x0, y0, radius, shape_error = fit_circle_(x_, y_)\n    x0, y0 = local_to_coordinates(x0, y0, x0_, y0_)\n    ax.plot(*build_circle(x0, y0, radius), label=\"circle\", color=\"red\", lw=0.5)\n    if j == 16:\n        break\n    j += 1"
      ]
    }
  ],
  "metadata": {
    "kernelspec": {
      "display_name": "Python 3",
      "language": "python",
      "name": "python3"
    },
    "language_info": {
      "codemirror_mode": {
        "name": "ipython",
        "version": 3
      },
      "file_extension": ".py",
      "mimetype": "text/x-python",
      "name": "python",
      "nbconvert_exporter": "python",
      "pygments_lexer": "ipython3",
      "version": "3.7.9"
    }
  },
  "nbformat": 4,
  "nbformat_minor": 0
}